package com.hang.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hang.entity.TbFile;
import com.hang.exception.MyException;
import com.hang.exception.MyExceptionEnum;
import com.hang.mapstruct.FileMapStruct;
import com.hang.model.dto.base.ConditionDTO;
import com.hang.model.dto.request.FileDeleteDTO;
import com.hang.model.dto.request.FileUploadDTO;
import com.hang.model.dto.request.ModuleConditionDTO;
import com.hang.model.vo.FileInfoListVO;
import com.hang.model.vo.FileInfoVO;
import com.hang.service.IFileService;
import com.hang.service.ITbFileService;
import com.hang.mapper.TbFileMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 *
 */
@Service
public class TbFileServiceImpl extends ServiceImpl<TbFileMapper, TbFile>
    implements ITbFileService{
    @Resource
    private IFileService fileService;

    @Resource
    private FileMapStruct fileMapStruct;

    /**
     * 删除文件及文件记录
     * @param deleteDTO
     */
    @Override
    public void deleteFileRecord(FileDeleteDTO deleteDTO) {
        Long fileId = deleteDTO.getFileId();
        Long userId = deleteDTO.getUserId();

        //校验是否有该文件可以删除
        QueryWrapper<TbFile> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.lambda().eq(TbFile::getFileId,fileId).eq(TbFile::getUserId,userId);
        Integer count = Optional.ofNullable(this.baseMapper.selectCount(fileQueryWrapper)).orElse(0);
        if(count < 1){
            throw new MyException(MyExceptionEnum.FILE_DELETE_ERROR);
        }

        //云端文件删除
        fileService.delete(this.getById(fileId).getFileUrl());

        //数据库中作逻辑删除
        this.removeById(fileId);
    }

    /**
     * 先上传，文件上传成功后返回url，再进行整体信息保存
     * @param uploadDTO
     */
    @Override
    public void saveFileRecord(FileUploadDTO uploadDTO) {
        TbFile file = fileMapStruct.FileUploadDTOToTbFile(uploadDTO);
        this.save(file);
    }

    /**
     * 文件列表接口
     * @param conditionDTO
     * @return
     */
    @Override
    public FileInfoListVO getFileListByCondition(ModuleConditionDTO conditionDTO) {
        if(ObjectUtil.isEmpty(conditionDTO)){
            throw new MyException(MyExceptionEnum.OBJECT_IS_NULL);
        }

        Long moduleId = conditionDTO.getModuleId();

        Integer pageNum = conditionDTO.getPageNum();
        Integer pageSize = conditionDTO.getPageSize();
        String condition = Optional.ofNullable(conditionDTO.getCondition()).orElse("");

        QueryWrapper<TbFile> fileQueryWrapper = new QueryWrapper<>();

        fileQueryWrapper.lambda().eq(TbFile::getModuleId,moduleId);
        if(StrUtil.isNotBlank(condition)){
            fileQueryWrapper.and(
                    wrapper -> wrapper
                            .lambda()
                            .like(TbFile::getFileName,condition)
                            .or()
                            .like(TbFile::getRealName,condition)
            );
        }

        fileQueryWrapper.lambda().orderByDesc(TbFile::getCreateTime);

        Page<TbFile> page = new Page<>(pageNum,pageSize);
        List<TbFile> fileList = Optional.ofNullable(this.baseMapper.selectPage(page, fileQueryWrapper).getRecords()).orElse(new LinkedList<>());

        //转换
        List<FileInfoVO> fileInfoVOList = fileMapStruct.fileListToFileInfoVOList(fileList);

        FileInfoListVO fileInfoListVO = new FileInfoListVO();
        fileInfoListVO.setTotal(Optional.ofNullable(this.baseMapper.selectCount(fileQueryWrapper)).orElse(0));
        fileInfoListVO.setFileInfoVOList(fileInfoVOList);

        return fileInfoListVO;
    }
}




